<template>
  <div>
    <el-switch
      v-if="row.key == 'local'"
      v-model="status"
      :disabled="row.status == 1"
      :active-value="1"
      :inactive-value="0"
      @change="changeStatus"
    />
    <el-switch
      v-else
      v-model="status"
      :disabled="disabled"
      :active-value="1"
      :inactive-value="0"
      @change="changeStatus"
    />
  </div>
</template>

<script>
export default {
  props: {
    row: {
      type: Object,
      default: (_) => {
        return {}
      }
    }
  },
  inject: ['reload'],
  data() {
    return {
      disabled: false,
      status: this.row.status
    }
  },
  methods: {
    changeStatus: function(value) {
      const txt = value
        ? '启用前请先确认配置信息是否正确，否则将导致无法唤起第三方登录功能'
        : '关闭该登录方式后，所有端将无法使用该第三方登录到本系统'
      this.$box
        .confirm(txt)
        .then(() => {
          const data = {
            status: value ? 1 : 0
          }
          this.disabled = true
          this.$http.post(
            '/admin/system/package/login/' + this.row.key + '/status',
            data,
            (response) => {
              this.disabled = false
            },
            (error) => {
              this.disabled = false
              this.$message.error(error.msg)
            },
            false
          )
        })
        .catch(() => {
          this.status = value == 1 ? 0 : 1
        })
    }
  }
}
</script>

<style></style>
